package com.duleme.servlet;

import com.duleme.common.EncodeUtils;
import com.duleme.common.JsonUtils;
import com.duleme.entity.BookJson;

import com.duleme.entity.Book;
import com.duleme.entity.Shop;
import com.duleme.service.impl.BookServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/data/shopBooks")
public class ShopGoodsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);
        EncodeUtils.setEncoding(req,resp);
        Shop shop = (Shop) req.getSession().getAttribute("shop");
        System.out.println(shop.getShopId());
        Integer ownerId = shop.getOwnerId();
        BookServiceImpl service=new BookServiceImpl();
        List<Book> list=service.booksOfShop(ownerId);
        if(list!=null){
            BookJson<Book> bookJson=new BookJson<>();
            bookJson.setData(list);
            bookJson.setCount(list.size());
            System.out.println(JsonUtils.toJson(bookJson));
            resp.getWriter().print(JsonUtils.toJson(bookJson));
        }else{
            resp.getWriter().print(0);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    public static void main(String[] args){

    }
}
